package cn.maihe.elg.operation.web.system;

import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.model.enums.UserStatusEnum;
import cn.maihe.elg.operation.model.enums.UserType;
import cn.maihe.elg.operation.model.vo.system.CreateUserVO;
import cn.maihe.elg.operation.model.vo.system.UpdateUserVO;
import cn.maihe.elg.operation.repository.entity.entityCustom.UserDOCustom;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.system.OrganizationService;
import cn.maihe.elg.operation.service.system.UserService;
import cn.maihe.elg.operation.utils.OrgCodeHandler;
import cn.maihe.elg.operation.utils.UserContextUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author JHWei
 * @date 2018/3/18
 */
@RestController
@Api(value = "用户管理接口", tags = {"用户管理"})
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private OrgCodeHandler orgCodeHandler;

    @Resource
    private ElgConfig elgConfig;
    @Resource
    private OrganizationService organizationService;

    @ApiOperation(value = "用户列表查询接口", notes = "根据登录账号(模糊查找)、用户名(模糊查找)、状态查找用户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "username", value = "登录账号(模糊查找)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "用户名(模糊查找)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "用户类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态->NORMAL:正常,DELETED:被删,LOCKED:被被锁", dataType = "String", paramType = "query", allowableValues = "NORMAL, DELETED, LOCKED")
    })
    @RequestMapping(value = "/api/users", method = RequestMethod.GET)
    public PageInfo<UserDOCustom> findAll(int pageNo, int pageSize, String username, String name, String status, String orgCode, Integer type) {
        log.info("查询用户信息开始");
//        if (StringUtils.isEmpty(status)) {
//            status = UserStatusEnum.NORMAL.toString();
//        }
        orgCode = orgCodeHandler.getCode(orgCode);
        return userService.getUsersDoPage(username, name, type, pageNo, pageSize, status, orgCode);
    }

    @ApiOperation(value = "获取用户信息接口", notes = "根据用户id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
    public UserDOCustom getUser(@PathVariable Long id) {
        UserDOCustom u = userService.getUser(id);
        u.setPassword(null);
        return u;
    }

    @ApiOperation(value = "获取用户信息接口", notes = "根据用户账户获取用户信息")
    @ApiImplicitParam(name = "userName", value = "用户id", dataType = "String", paramType = "path", required = true)
    @RequestMapping(value = "/api/userInfo/{userName}", method = RequestMethod.GET)
    public UserDOCustom getUserInfo(@PathVariable String userName) {
        UserDOCustom u = userService.getByUsername(userName);
        u.setPassword(null);
        return u;
    }

    @ApiOperation(value = "新增用户信息接口", notes = "新增用户信息")
    @RequestMapping(value = "/api/user", method = RequestMethod.POST)
    public void createUser(@Validated @RequestBody CreateUserVO createUserVO) {
        UserDOCustom user = new UserDOCustom();
        BeanUtils.copyProperties(createUserVO,user);

        UserContext context = UserContextUtil.getUserContext();
        user.setOrgCode(context.getOrgCode());
        user.setOrgId(context.getOrgId());
        user.setType(createUserVO.getUserType());
//        user.setType(context.getUserType()== UserType.GENERAL.getType() ?UserType.GENERAL.getType():UserType.ACCOUNT.getType());

                //user.setPassword("a123123");
        userService.saveUser(user);
    }

    @ApiOperation(value = "修改用户信息接口", notes = "修改用户信息")
    @RequestMapping(value = "/api/user", method = RequestMethod.PUT)
    public void modifyUser(@Validated @RequestBody UpdateUserVO updateUserVO) {
        UserDOCustom user = new UserDOCustom();
        BeanUtils.copyProperties(updateUserVO,user);
        userService.updateUser(user);
    }

    @ApiOperation(value = "删除用户信息接口", notes = "根据用户id删除用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/api/user/{id}", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable Long id) {
        log.info("删除用户开始！！！");
        userService.deleteUser(id);
    }

    @ApiOperation(value = "修改用户信息接口", notes = "修改用户信息")
    @RequestMapping(value = "/api/userInfo", method = RequestMethod.PUT)
    public void modifyUserInfo(@Validated @RequestBody UpdateUserVO updateUserVO) {

        UserDOCustom user = new UserDOCustom();
        BeanUtils.copyProperties(updateUserVO,user);
        userService.updateUserInfo(user);

    }

    @ApiOperation(value = "修改用户密码接口", notes = "根据登录账号修改用户密码")
    @ApiImplicitParam(name = "param", value = "json格式 例如:{\"username\":登录账号,\"rawpassword\":原密码,\"password\":新密码}", dataType = "Map", paramType = "body", required = true)
    @RequestMapping(value = "/api/user/password", method = RequestMethod.PUT)
    public void changePassword(@RequestBody Map<String, String> param) {
        userService.changePassword(param);
    }

    @ApiOperation(value = "重置用户密码接口", notes = "根据用户id重置用户密码")
    @ApiImplicitParam(name = "param", value = "json格式 例如:{\"id\":用户id,\"password\":新密码}", dataType = "Map", paramType = "body", required = true)
    @RequestMapping(value = "/api/user/reset/password", method = RequestMethod.PUT)
    public void resetPassword(@RequestBody Map<String, String> param) {
        Long id = Long.parseLong(param.get("id"));
        String password = param.get("password");
        userService.resetPassword(id, password);
    }

    @ApiOperation(value = "解锁用户接口", notes = "根据用户id解锁用户")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/api/user/unlock/{id}", method = RequestMethod.PUT)
    public void unlock(@PathVariable Long id) {
        userService.unLock(id);
    }



}
